﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Brain.Windows;
using Brain.Editor;
using Brain.Build;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Brain
{
    public partial class ConversorForm : Form
    {
        public ConversorForm()
        {
            InitializeComponent();
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            this.list_Log.Items.Clear();
        }

        private void button_SearchFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();

            if (open.ShowDialog() == DialogResult.OK)
            {
                this.textBox_FileName.Text = open.FileName;
            }
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();

            if (save.ShowDialog() == DialogResult.OK)
            {
                this.textBox_SavePlace.Text = save.FileName;
            }
        }

        private bool converting;
        private void button_Convert_Click(object sender, EventArgs e)
        {
            if (!converting)
            {
                converting = true;
                ThreadPool.QueueUserWorkItem(this.Convert);
            }
        }

        private void Convert(object o)
        {
            string fileName = PropertyValue.GetControlPropertyValue<string>(this.textBox_FileName, "Text").ToLower();
            string savePlace =PropertyValue.GetControlPropertyValue<string>(this.textBox_SavePlace, "Text").ToLower();
            Log("Started converting file " + fileName);

            string extension = Path.GetExtension(fileName).ToLower();

            Log("Extension is " + extension);

            if (extension.Contains("xnb"))
            {
                Log("File is already compiled. The conversor can't handle files like this currently");
            }
            else
            {
                // File is not compiled
                Log("File needs to be compiled");

                ContentBuilder Builder = new ContentBuilder();

                if (!extension.Contains(".bmodel") || !extension.Contains(".bimg"))
                {
                    // We need to import the file Using XNA
                    string processor;
                    string importer;
                    ContentFileUtil.InsertDefaultProcessors(extension, out processor, out importer);

                    Builder.Add(fileName, "name", importer, processor, null);
                    string error = Builder.Build();
                    if (!string.IsNullOrEmpty(error))
                    {
                        MessageBox.Show(error);
                    }
                    else
                    {
                        if (extension.Contains(".x") || extension.Contains(".fbx"))
                        {
                            ContentManager manager = new ContentManager(Engine.Instance.ServiceProvider, Builder.OutputDirectory);
                            Model m = manager.Load<Model>("name");

                            Log("Model loaded. Converting to Brain Model...");

                            Vector3[] vertices;
                            int[] indices;
                            TriangleUtil.GetVerticesAndIndicesFromModel(m, out vertices, out indices);

                            Log("Model data extracted. Saving as Brain Model...");

                            using (FileStream stream = new FileStream(savePlace + ".bmodel", FileMode.CreateNew))
                            {
                                //BrainModel.Write(stream, vertices, indices);
                            }

                            Log("Brain Model data saved. Cleaning up...");

                            manager.Dispose();

                            Log("Done!");
                        }
                    }
                }
                Builder.Dispose();
            }

            converting = false;
        }

        public string Add
        {
            set
            {
                this.list_Log.Items.Add(value);
            }
        }

        public void Log(string s)
        {
            PropertyValue.SetControlPropertyValue(this, "Add", s);
        }


    }
}
